﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YRevitJig;

namespace YArchitech.HVAC
{
	public class XDimensionJig : XUiJigBase
	{
		public XDimensionJig(UIDocument uiDoc, Line dimLine, List<XYZ> point) : base(uiDoc)
		{
			this.dimLine = dimLine;
			this.points = point;
			this.JigStage = XDimensionJig.EStage.eNoOper;
		}

		public XYZ PickDimensionPoint(string strPrompt = "请确定标注点:")
		{
			XYZ xyz = null;
			this.JigStage = XDimensionJig.EStage.ePickPt;
			try
			{
				xyz = base.UiDoc.Selection.PickPoint(strPrompt);
			}
			catch (Exception ex)
			{
				throw ex;
			}
			XYZ xyzpoint = this.dimLine.Project(xyz).XYZPoint;
			return xyz - new XYZ(xyzpoint.X, xyzpoint.Y, xyz.Z);
		}

		public override void Draw(PaintEventArgs paintEventArgs)
		{
			base.Draw(paintEventArgs);
			if (!this.m_ptCur.IsEmpty && this.JigStage == XDimensionJig.EStage.ePickPt)
			{
				XGraphics xgraphics = new XGraphics(paintEventArgs.Graphics, base.JigPen);
				List<XYZ> source = this.GetProjectPoints(this.points);
				XYZ xyz = base.Client2Revit(this.m_ptCur);
				if (xyz == null)
				{
					return;
				}
				XYZ xyzpoint = this.dimLine.Project(xyz).XYZPoint;
				if (this.dimLine.Direction.X != 0.0)
				{
					source = (from e in source
					orderby e.X
					select e).Cast<XYZ>().ToList<XYZ>();
				}
				else
				{
					source = (from e in source
					orderby e.Y
					select e).Cast<XYZ>().ToList<XYZ>();
				}
				XLine2D xline2D = new XLine2D(base.Revit2Client(source.FirstOrDefault<XYZ>() + xyz - xyzpoint), base.Revit2Client(source.Last<XYZ>() + xyz - xyzpoint));
				xline2D.Draw(xgraphics);
				this.DrawLinkLine(source.FirstOrDefault<XYZ>() + xyz - xyzpoint, source.Last<XYZ>() + xyz - xyzpoint, xline2D, xgraphics);
			}
		}

		private void DrawLinkLine(XYZ start, XYZ end, XLine2D line, XGraphics XGraphics)
		{
			if (start.IsAlmostEqualTo(end))
			{
				return;
			}
			Line line2 = Line.CreateBound(start, end);
			foreach (XYZ xyz in this.points)
			{
				double num = 0.5;
				line = new XLine2D(base.Revit2Client(line2.Project(xyz).XYZPoint), base.Revit2Client(xyz));
				line.Draw(XGraphics);
				XYZ xyz2 = line2.Project(xyz).XYZPoint - new XYZ(xyz.X, xyz.Y, line2.Project(xyz).XYZPoint.Z);
				XYZ xyz3 = line2.Project(xyz).XYZPoint + num / 2.0 * xyz2.Normalize();
				XYZ xyz4 = line2.Project(xyz).XYZPoint - num / 2.0 * xyz2.Normalize();
				DetailCurve detailCurve = base.Doc.Create.NewDetailCurve(base.Doc.ActiveView, Line.CreateBound(xyz3, xyz4));
				ElementTransformUtils.RotateElement(base.Doc, detailCurve.Id, Line.CreateUnbound(line2.Project(xyz).XYZPoint, new XYZ(0.0, 0.0, 1.0)), -0.78539816339744828);
				line = new XLine2D(base.Revit2Client(((LocationCurve)detailCurve.Location).Curve.GetEndPoint(0)), base.Revit2Client(((LocationCurve)detailCurve.Location).Curve.GetEndPoint(1)));
				line.Draw(XGraphics);
				base.UiDoc.Document.DeleteElement(detailCurve);
			}
		}

		private List<XYZ> GetProjectPoints(List<XYZ> points)
		{
			List<XYZ> list = new List<XYZ>();
			foreach (XYZ xyz in points)
			{
				list.Add(this.dimLine.Project(xyz).XYZPoint);
			}
			return list;
		}

		public XDimensionJig.EStage JigStage { get; set; }

		private Line dimLine;

		private List<XYZ> points = new List<XYZ>();

		public enum EStage
		{
			ePickPt,
			eNoOper
		}
	}
}
